<template>
  <div style="display: flex; justify-content: flex-end">
    <el-form :model="queryParams" class="filter-form-inline" label-position="right" size="small">
      <el-row :gutter="40" style="margin-left: 0px; margin-right: 0px">
        <el-form-item label="产品信息" style="width:300px;margin-right:20px;">
          <el-select v-model="queryParams.productId" :loading="loading" :remote-method="remoteMethod" clearable filterable
            placeholder="请选择产品" remote reserve-keyword>
            <el-option v-for="item in products" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button class="border-btn" @click="handlReset">重置
          </el-button>
          <el-button class="main-btn" type="primary" @click="handlSearch">搜索
          </el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </div>

  <el-table header-cell-class-name="table-header" :data="tableData" style="width: 100%;" border>
    <el-table-column label="跟进时间" show-overflow-tooltip header-align="center" fixed="left" :min-width="140">
      <template #default="{row}">
        {{ dateFormat("yyyy-mm-dd HH:MM:SS", row.updatedAt) }}
      </template>
    </el-table-column>

    <el-table-column prop="headName" label="跟进职员" show-overflow-tooltip align="center" fixed="left" :min-width="100" />

    <el-table-column prop="productPopularName" label="产品通用名" show-overflow-tooltip header-align="center" fixed="left"
      :min-width="160" />

    <el-table-column prop="productName" label="商品名" show-overflow-tooltip header-align="center" :min-width="160" />

    <el-table-column prop="packSpecs" label="包装规格" show-overflow-tooltip header-align="center" :min-width="140" />

    <el-table-column prop="producer" label="生产厂家" show-overflow-tooltip header-align="center" :min-width="140" />

    <el-table-column label="跟进形式" show-overflow-tooltip align="center" :min-width="140">
      <template #default="scope">
        {{getLabel('visit_form',scope.row.visitForm)}}
      </template>
    </el-table-column>

    <el-table-column prop="content" label="跟进内容" show-overflow-tooltip header-align="center" :min-width="350" />

    <el-table-column prop="contactName" label="客户联系人" show-overflow-tooltip align="center" fixed="right"
      :min-width="100" />
  </el-table>
</template>

<script>
import {
  defineAsyncComponent,
  getCurrentInstance,
  inject,
  onMounted,
  reactive,
  toRefs,
} from "vue";
import { useRouter } from "vue-router";
import {getProductName} from "@/utils/commonFun";
export default {
  name: "followUp",
  props: {
    objectId: {
      type: Number,
      default: 0,
    },
    customerType: {
      type: String,
      default: "",
    },
  },
  setup(props) {
    const api = inject("api");
    const router = useRouter();
    const currentInstance = getCurrentInstance();
    const data = reactive({
      queryParams: {
        objectId: props.objectId,
        customerType: props.customerType,
        productId: undefined,
      },
      tableData: [],
      loading: false,
      products: [],
      searchStr: "",
    });

    const handleList = () => {
      api.channelAgent.QUALIFICATION_LISTTYPE(data.queryParams).then((res) => {
        if (res.code === 0) {
          data.tableData = res.data.list || [];
        }
      });
    };

    const remoteMethod = (query) => {
      data.products = [];
      data.loading = true;
      data.searchStr = query;

      if (query) {
        const pam = {
          keyWord: query,
        };
        api.common.GET_PRODUCT_LIST(pam).then((res) => {
          if (!res.code && res.data) {
            res.data.list.forEach((a) => {
              a.name = getProductName(a);
            });
            data.products = res.data.list;
          } else {
            currentInstance.proxy.customMessage({
              type: "error",
              message: res.msg,
            });
          }
          data.loading = false;
        });
      } else {
        data.loading = false;
      }
    };

    const handlReset = () => {
      data.queryParams.productId = undefined;
      handleList();
    };

    const handlSearch = () => {
      handleList();
    };

    onMounted(() => {
      handleList();
    });

    return {
      ...toRefs(data),
      handleList,
      remoteMethod,
      handlReset,
      handlSearch,
    };
  },
};
</script>

<style lang="scss" scoped>
</style>